#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：cold_rolling_new 
@File ：SchedulingSystemBase.py
@IDE  ：PyCharm 
@Author ：李涵彬
@Date ：2024/12/30 上午9:33 
"""

from abc import ABC, abstractmethod
from typing import Any, Dict, List

import pandas as pd


class SchedulingSystemBase(ABC):
	def __init__(self, line_no: str):
		self.line_no = line_no
		self.roll_plan_number = line_no + "_" + self.get_time('%Y%m%d%H%M%S')
		self.data = None
		self.config = self.load_config()

	@staticmethod
	def get_time(form: str = '%Y-%m-%d %H:%M:%S') -> str:
		"""获取当前时间"""
		from datetime import datetime
		return datetime.now().strftime(form)

	@abstractmethod
	def load_config(self) -> Dict[str, Any]:
		"""加载产线配置文件"""
		pass

	@abstractmethod
	def get_and_process_data(self) -> pd.DataFrame:
		"""数据获取与处理"""
		pass

	@abstractmethod
	def process_data(self) -> None:
		"""使用进程池进行数据处理"""
		pass

	@abstractmethod
	def filter_data(self) -> None:
		"""数据筛选"""
		pass

	@abstractmethod
	def call_algorithm(self) -> None:
		"""调用算法"""
		pass

	@abstractmethod
	def write_to_database(self) -> None:
		"""数据库写入"""
		pass

	@abstractmethod
	def save_data(self, data: pd.DataFrame, data_name: str) -> None:
		"""保存数据到CSV文件"""
		pass

	def entry(self) -> str:
		"""排程入口"""
		try:
			# 总流程控制
			self.get_and_process_data()
			self.process_data()
			self.filter_data()
			self.call_algorithm()
			self.write_to_database()
			return f"{self.line_no}产线程序执行成功"
		except Exception as e:
			return f"{self.line_no}产线程序执行出错: {e}"
